home *** CD-ROM | disk | FTP | other *** search
- /*
- * PostCard, version 1.0, Lightspeed C
- * demonstrates using data forks and resource tricks
- *
- * Copyright 1987 by Joel McNamara - All Rights Reserved
- * for MacTutor Magazine
- *
- * initial coding - October 12, 1987
- */
-
- #include "MacTypes.h"
- #include "QuickDraw.h"
- #include "WindowMgr.h"
- #include "EventMgr.h"
- #include "MenuMgr.h"
- #include "FileMgr.h"
- #include "MemoryMgr.h"
- #include "ResourceMgr.h"
- #include "StdFilePkg.h"
- #include "OSUtil.h"
- #include "DialogMgr.h"
-
-
- /* defines... */
- #define MessageDLOG 255
- #define AboutDLOG 256
- #define HelpDLOG 257
-
- #define AppleMENU 1
- #define About 1
-
- #define FileMENU 255
- #define Quit 1
-
- #define CommandMENU 256
- #define MakeCard 1
- #define Help 3
-
-
- /* globals... */
- Boolean done;
- char theString[256];
- MenuHandle AppleMenuHndl, FileMenuHndl, CommandMenuHndl;
- EventRecord theEvent;
- DialogPtr theDialog;
- int resRef,dummy,myError;
-
-
- /* main */
- main()
- {
- Initialize();
- do{
- MainLoop();
- }while(!done);
- }
-
-
- /* as always... */
- Initialize()
- {
- InitGraf(&thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0);
- InitCursor();
- FlushEvents(everyEvent, 0);
-
- SetUpMenus();
-
- done = FALSE;
- myError = 0;
- }
-
-
- /* make the menus */
- SetUpMenus()
- {
- AppleMenuHndl = GetMenu(AppleMENU);
- AddResMenu(AppleMenuHndl, 'DRVR');
- InsertMenu(AppleMenuHndl, 0);
-
- FileMenuHndl = GetMenu(FileMENU);
- InsertMenu(FileMenuHndl, 0);
-
- CommandMenuHndl = GetMenu(CommandMENU);
- InsertMenu(CommandMenuHndl, 0);
-
- DrawMenuBar();
- }
-
-
- /* generic dialog displayer */
- DoDialog(theDLOG)
- int theDLOG;
- {
- theDialog = GetNewDialog(theDLOG, 0, -1);
- DoButton(theDialog);
- ModalDialog(0, &dummy);
- DisposDialog(theDialog);
- }
-
-
- /* message display routine */
- DoMessage(theMessage)
- Str255 theMessage;
- {
- theDialog = GetNewDialog(MessageDLOG, 0, -1);
- ParamText(theMessage, "\p", "\p", "\p");
- DoButton(theDialog);
- ModalDialog(0, &dummy);
- DisposDialog(theDialog);
- }
-
-
- /* loop until we're done */
- MainLoop()
- {
- SystemTask();
- if (GetNextEvent(everyEvent,&theEvent)) {
- switch (theEvent.what) {
- case mouseDown:
- DoMouseDown();
- break;
-
- default:
- break;
- }
- }
- }
-
-
- /* if we have a mouse down */
- DoMouseDown()
- {
- WindowPtr whichWindow;
- int thePart;
-
- thePart = FindWindow(theEvent.where,&whichWindow);
- switch(thePart) {
- case inMenuBar:
- DoMenu();
- break;
-
- case inSysWindow:
- SystemClick(&theEvent, whichWindow);
- break;
-
- default:
- break;
- }
- }
-
-
- /* if a menu hit then... */
- DoMenu()
- {
- long menuChoice;
-
- menuChoice = MenuSelect(theEvent.where);
- DoMenuItem(menuChoice);
- }
-
-
- /* which menu was selected? */
- DoMenuItem(menuChoice)
- long menuChoice;
- {
- int theMenu,theItem;
-
- if (menuChoice != 0 ) {
- theItem = menuChoice;
- theMenu = (menuChoice >> 16);
-
- switch (theMenu) {
- case AppleMENU:
- DoDA(theItem);
- break;
-
- case FileMENU:
- done = TRUE;
- break;
-
- case CommandMENU:
- DoCommands(theItem);
-
- default:
- break;
- }
- HiliteMenu(0);
- }
- }
-
-
- /* the apple menu was hit */
- DoDA(theItem)
- int theItem;
- {
- int accNumber;
-
- if (theItem == About)
- DoDialog(AboutDLOG);
- else{
- GetItem(AppleMenuHndl,theItem,theString);
- accNumber = OpenDeskAcc(theString);
- }
- }
-
-
- /* the commands menu was hit */
- DoCommands(theItem)
- int theItem;
- {
- switch(theItem) {
- case MakeCard:
- ConvertFile();
- break;
-
- case Help:
- DoDialog(HelpDLOG);
- break;
-
- default:
- break;
- }
- }
-
-
- /* make a postcard */
- ConvertFile()
- {
- char* buffer;
- long theEOF;
- int currentResRef,refNum,destRef;
- Handle codeHandle;
-
- Point myPoint;
- SFReply paintReply, myReply;
- SFTypeList myTypes;
- FInfo myFInfo;
- OSErr myErr;
-
- myTypes[0] = 'PNTG';
- myPoint.h = 90;
- myPoint.v = 90;
-
- /* first we create the application */
- DoMessage("\pFirst name the PostCard you want to create.");
- SFPutFile(myPoint, "\pEnter PostCard name:", "\pMy PostCard", 0,
- &myReply);
- if (myReply.good)
- {
- SetVol(0, myReply.vRefNum);
- CreateResFile(myReply.fName);
-
- /* make it what we want... */
- myFInfo.fdType = 'APPL';
- myFInfo.fdCreator = 'PDIS';
- myFInfo.fdFlags = fHasBundle;
- myFInfo.fdLocation.h = 0;
- myFInfo.fdLocation.v = 0;
- SetFInfo(myReply.fName, myReply.vRefNum, &myFInfo);
-
- /* open its resource fork */
- resRef = OpenResFile(myReply.fName);
- if (resRef != -1)
- {
- /* get PostCard's resource ref number and then
- start moving the resources */
- currentResRef = CurResFile();
-
- /* first the obvious ones */
- ResTransfer('BNDL',129, 'BNDL',128);
- ResTransfer('PDIS',0,'PDIS',0);
- ResTransfer('FREF',128,'FREF',128);
- ResTransfer('ICN#',129,'ICN#',128);
-
- /* now we grab the FAKEs and make them CODE */
- ResTransfer('FAKE',0,'CODE',0);
- ResTransfer('FAKE',1,'CODE',1);
- ResTransfer('FAKE',2,'CODE',2);
-
- /* finally get the DUMYs and convert them */
- ResTransfer('DUMY',0,'CREL',2);
- ResTransfer('DUMY',1,'DATA',0);
- ResTransfer('DUMY',2,'DREL',0);
- ResTransfer('DUMY',3,'STRS',0);
- ResTransfer('DUMY',4,'ZERO',0);
-
- /* update the new application, close it, make PostCard
- the current open resource fork */
- UpdateResFile(resRef);
- CloseResFile(resRef);
- UseResFile(currentResRef);
-
- /* see if we have any errors - open the application */
- if ( (FSOpen(myReply.fName, myReply.vRefNum, &destRef) != noErr) ||
- (myError == 1) ) {
- DoMessage("\pSorry a resource error prevented the PostCard from being created.");
- FSDelete(myReply.fName,myReply.vRefNum);
- myError = 0;
- }
- else
- {
- /* here we select the paint file we want to convert */
- DoMessage("\pNow select the Paint file you want to display.");
- SFGetFile(myPoint, "\p", 0, 1, myTypes, 0, &paintReply);
- if (paintReply.good)
- {
- /* open, read, and write into the document */
- if (FSOpen(paintReply.fName, paintReply.vRefNum, &refNum) == noErr)
- {
- if ( GetEOF(refNum, &theEOF) != noErr)
- myError = 1;
- buffer = NewPtr(theEOF);
- if ( FSRead(refNum, &theEOF, buffer) != noErr)
- myError = 1;
- if ( FSWrite(destRef, &theEOF, buffer) != noErr)
- myError = 1;
- myErr = FSClose(refNum);
- myErr = FSClose(destRef);
- if (myError != 1)
- DoMessage("\pYour PostCard has been created!");
- }
- }
- else
- {
- /* get rid of file we've created */
- FSDelete(myReply.fName,myReply.vRefNum);
- }
- }
-
- }
- else
- myError = 1;
-
- if (myError == 1) {
- /* opps, problems - blitz the file */
- DoMessage("\pSorry an error prevented the PostCard from being created.");
- FSDelete(myReply.fName,myReply.vRefNum);
- }
- }
- }
-
-
- /* grab a resource and rename it routine */
- ResTransfer(sourceRes, sourceID, destRes, destID)
- ResType sourceRes, destRes;
- int sourceID, destID;
- {
- Handle codeHandle;
- codeHandle = NewHandle(0);
-
- /* get the source resource */
- codeHandle = GetResource(sourceRes,sourceID);
-
- /* set the current res file to the stand alone document */
- UseResFile(resRef);
-
- /* detach the source resource... */
- DetachResource(codeHandle);
-
- /* and add it to the document */
- AddResource(codeHandle, destRes, destID, "\p");
- if (ResError() != noErr) {
- DoMessage("\pSorry, couldn't copy the required resource.");
- myError = 1;
- }
- }
-
-
- /* fancy button for the modals */
- DoButton(myDialog)
- DialogPtr myDialog;
- {
- short myType;
- Handle myHandle;
- Rect myBox;
- GrafPtr dummyPort;
-
- GetPort(&dummyPort);
- SetPort(myDialog);
- GetDItem( myDialog, 1, &myType, &myHandle, &myBox );
- InsetRect(&myBox, -4, -4);
- PenSize(3, 3);
- FrameRoundRect(&myBox, 16, 16);
- PenNormal();
- SetPort(dummyPort);
- }
-
-